/*
 * $RCSfile: Oracle.java,v $
 * $Revision: 1.1 $
 * $Date: 2014-03-24 $
 *
 * Copyright (C) 2008 Skin, Inc. All rights reserved.
 *
 * This software is the proprietary information of Skin, Inc.
 * Use is subject to license terms.
 */
package com.skin.webcat.database.oracle;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.skin.webcat.util.Jdbc;

/**
 * <p>Title: MySql</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * @author xuesong.net
 * @version 1.0
 */
public class Oracle {
    /**
     * @param connection
     * @return String
     * @throws SQLException
     */
    public static OracleURL getURL(Connection connection) throws SQLException {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        String url = databaseMetaData.getURL();
        String userName = databaseMetaData.getUserName();
        OracleURL oracleURL = OracleURL.parse(url);

        if(oracleURL == null) {
            return null;
        }

        oracleURL.setUserName(userName);
        oracleURL.setPassword((String)null);
        return oracleURL;
    }

    /**
     * @param connection
     * @return String
     * @throws SQLException
     */
    public static String getUserName(Connection connection) throws SQLException {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        return databaseMetaData.getUserName();
    }

    /**
     * @param connection
     * @return List<String>
     * @throws SQLException
     */
    public static List<String> getSchemas(Connection connection) throws SQLException {
        ResultSet resultSet = null;
        DatabaseMetaData metaData = connection.getMetaData();

        try {
            resultSet = metaData.getSchemas();
            List<String> names = new ArrayList<String>();

            while(resultSet.next()) {
                names.add(resultSet.getString("TABLE_SCHEM"));
            }
            java.util.Collections.sort(names);
            return names;
        }
        finally {
            Jdbc.close(resultSet);
        }
    }
}
